package cn.edu.sjtu.se.dslab.component.method.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.edu.sjtu.se.dslab.component.method.InterfaceMethod;
import cn.edu.sjtu.se.dslab.component.method.MethodComponent;
import cn.edu.sjtu.se.dslab.service.IMethodComponentService;

@Service("methodComponentTool")
public class MethodComponentTool {
	@Resource(name = "methodComponentService")
	IMethodComponentService methodComponentService;
	
//	@Resource(name = "componentInterfaceService")
//	IComponentInterfaceService componentInterfaceService;
	/**
	 * 该函数用以建立methodcomponent和其所实现接口的函数之间的关系，用以通过函数引用该methodcomponent
	 * @return
	 */
	public List<MethodComponentLink> getComponentLinks(){
		List<MethodComponentLink> links = new ArrayList<MethodComponentLink>();
		List<MethodComponent> components = methodComponentService.getAllMethodComponents();
		for (MethodComponent methodComponent : components) {
			if (methodComponent.getCinterface() != null) {
				//ComponentInterface cInterface = componentInterfaceService.getComponentInterfaceByUri(methodComponent.getCinterface().getUri());
				Set<InterfaceMethod> methods = methodComponent.getCinterface().getInterfaceMethods();
				if (methods != null) {
					for (InterfaceMethod interfaceMethod : methods) {
						MethodComponentLink link = new MethodComponentLink();
						link.setComponent(methodComponent);
						link.setMethod(interfaceMethod);
						links.add(link);
					}
				}
			}
		}
		return links;
	}
	
	
	/**
	 * 该函数用以建立methodcomponent和其所实现接口的函数之间的关系，用以通过函数引用该methodcomponent
	 * 参数：接口类型
	 * @return
	 */
	public List<MethodComponentLink> getComponentLinksByInterfaceType(String type){
		List<MethodComponentLink> links = new ArrayList<MethodComponentLink>();
		List<MethodComponent> components = methodComponentService.getAllMethodComponents();
		for (MethodComponent methodComponent : components) {
			if (methodComponent.getCinterface() != null && methodComponent.getCinterface().getItype().equals(type)) {
				//ComponentInterface cInterface = componentInterfaceService.getComponentInterfaceByUri(methodComponent.getCinterface().getUri());
				Set<InterfaceMethod> methods = methodComponent.getCinterface().getInterfaceMethods();
				if (methods != null) {
					for (InterfaceMethod interfaceMethod : methods) {
						MethodComponentLink link = new MethodComponentLink();
						link.setComponent(methodComponent);
						link.setMethod(interfaceMethod);
						links.add(link);
					}
				}
			}
		}
		return links;
	}
}
